package mumu.android.lib.view;

import java.util.ArrayList;
import java.util.List;

import mumu.android.R;
import mumu.android.lib.view.TreeListView.ITreeInterface;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

public class TreePopView {
	public View v;
	List<ChannelEn> list = new ArrayList<ChannelEn>();
	LayoutInflater inflater;
	Context mContext;

	public class G {
		PopupWindow pWindow;
	}

	G g = new G();

	public interface ICataClick {
		void click(ChannelEn catalog);

	}

	ICataClick iICataClick;

	public void setICataClick(ICataClick iICataClick) {
		this.iICataClick = iICataClick;

	}

	class ViewHolder {
		TextView text;
		ImageView icon;

	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public View onCreateView(Context mcontent) {
		mContext = mcontent;
		this.inflater = LayoutInflater.from(mcontent);
		v = inflater.inflate(R.layout.tree, null);
		TreeListView treeListView = (TreeListView) v.findViewById(R.id.tlv);
		treeListView.onCreate(initdata(), new ITreeInterface<ChannelEn>() {

			@Override
			public View getView(TreeEntity<ChannelEn> t, int position, View convertView) {
				ViewHolder holder;
				// if (convertView == null) {
				convertView = LayoutInflater.from(mContext).inflate(R.layout.outline, null);
				holder = new ViewHolder();
				holder.text = (TextView) convertView.findViewById(R.id.text);
				holder.icon = (ImageView) convertView.findViewById(R.id.icon);
				convertView.setTag(holder);
				// } else {
				// holder = (ViewHolder) convertView.getTag();
				// }

				int level = t.Level;
				holder.icon.setPadding(25 * (level + 1), holder.icon.getPaddingTop(), 0, holder.icon.getPaddingBottom());
				holder.text.setText(t.Data.ChannelName);

				if (t.HasChild && !t.IsExpanded) {
					holder.icon.setImageResource(R.drawable.outline_list_collapse);
				} else if (t.HasChild && t.IsExpanded) {
					holder.icon.setImageResource(R.drawable.outline_list_expand);
				} else if (t.HasChild) {
					holder.icon.setImageResource(R.drawable.outline_list_collapse);
					// holder.icon.setVisibility(View.INVISIBLE);
				} else {
					holder.icon.setImageResource(R.drawable.outline_list_collapse);
					holder.icon.setVisibility(View.INVISIBLE);
				}
				return convertView;
			}

			@Override
			public void onItemClick(TreeEntity<ChannelEn> t, int position) {
				Toast.makeText(mContext, "点击了", 1000).show();
			}
		});
		return v;
	}

	List<TreeEntity<ChannelEn>> initdata() {
		ChannelEn c = new ChannelEn();
		c.ChannelId = 1;
		c.ChannelParentId = 0;
		c.ChannelPicture = "";
		c.HasChild = 1;
		c.ChannelName = "第一2";

		ChannelEn f = new ChannelEn();
		f.ChannelId = 2;
		f.ChannelParentId = 0;
		f.ChannelName = "第一1";
		f.HasChild = 0;

		ChannelEn e = new ChannelEn();
		e.ChannelId = 3;
		e.ChannelParentId = 1;
		e.ChannelName = "第二1";
		e.HasChild = 0;

		ChannelEn h = new ChannelEn();
		h.ChannelId = 4;
		h.ChannelParentId = 1;
		h.ChannelName = "第二2";
		h.HasChild = 0;

		ChannelEn g = new ChannelEn();
		g.ChannelId = 5;
		g.ChannelParentId = 1;
		g.ChannelName = "第二3";
		g.HasChild = 1;

		ChannelEn i = new ChannelEn();
		i.ChannelId = 6;
		i.ChannelParentId = 5;
		i.ChannelName = "第三1";
		i.HasChild = 0;

		ChannelEn j = new ChannelEn();
		j.ChannelId = 7;
		j.ChannelParentId = 5;
		j.ChannelName = "第三2";
		j.HasChild = 0;

		TreeEntity<ChannelEn> ce = new TreeEntity<ChannelEn>();
		ce.IsExpanded = false;
		ce.Id = c.ChannelId;
		ce.ParentId = c.ChannelParentId;
		ce.HasChild = c.HasChild == 1;
		ce.Data = c;

		TreeEntity<ChannelEn> fe = new TreeEntity<ChannelEn>();
		fe.IsExpanded = false;
		fe.Id = f.ChannelId;
		fe.ParentId = f.ChannelParentId;
		fe.HasChild = f.HasChild == 1;
		fe.Data = f;

		TreeEntity<ChannelEn> ee = new TreeEntity<ChannelEn>();
		ee.IsExpanded = false;
		ee.Id = e.ChannelId;
		ee.ParentId = e.ChannelParentId;
		ee.HasChild = e.HasChild == 1;
		ee.Data = e;

		TreeEntity<ChannelEn> hh = new TreeEntity<ChannelEn>();
		hh.IsExpanded = false;
		hh.Id = h.ChannelId;
		hh.ParentId =h.ChannelParentId;
		hh.HasChild =h.HasChild == 1;
		hh.Data = h;

		TreeEntity<ChannelEn> gg = new TreeEntity<ChannelEn>();
		gg.IsExpanded = false;
		gg.Id = g.ChannelId;
		gg.ParentId = g.ChannelParentId;
		gg.HasChild = g.HasChild == 1;
		gg.Data =g;

		TreeEntity<ChannelEn> ii = new TreeEntity<ChannelEn>();
		ii.IsExpanded = false;
		ii.Id = i.ChannelId;
		ii.ParentId = i.ChannelParentId;
		ii.HasChild = i.HasChild == 1;
		ii.Data =i;

		TreeEntity<ChannelEn> jj = new TreeEntity<ChannelEn>();
		jj.IsExpanded = false;
		jj.Id = j.ChannelId;
		jj.ParentId = j.ChannelParentId;
		jj.HasChild = j.HasChild == 1;
		jj.Data = j;

		List<TreeEntity<ChannelEn>> list = new ArrayList<TreeEntity<ChannelEn>>();
		list.add(fe);
		list.add(ee);
		list.add(ce);
		list.add(gg);
		list.add(hh);
		list.add(ii);
		list.add(jj);

		return list;

	}

	public void showPopWindow() {
		try {
			g.pWindow = new PopupWindow(v, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			// 设置如下四条信息，当点击其他区域使其隐藏，要在show之前配置
			g.pWindow.setFocusable(true);
			g.pWindow.setOutsideTouchable(true);
			g.pWindow.update();
			g.pWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
			// 设置SelectPicPopupWindow弹出窗体动画效果
			g.pWindow.setAnimationStyle(android.R.style.Animation_Dialog);
			g.pWindow.showAtLocation(((Activity) mContext).getWindow().getDecorView().findViewById(android.R.id.content).getRootView(), Gravity.CENTER, 0, 0);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
